package org.dev.biz_login

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_login.*
import org.dev.`as`.project.common.http.ApiFactory
import org.dev.`as`.project.common.route.HiRoute
import org.dev.`as`.project.common.route.RouteFlag
import org.dev.`as`.project.common.ui.component.HiBaseActivity
import org.dev.biz_login.api.AccountApi
import org.dev.hi.library.restful.HiCallback
import org.dev.hi.library.restful.HiResponse
import org.dev.hi.library.util.HiStatusBar

/**
 * Author WenPing
 * CreateTime 2021/5/9.
 * Description:登录页面
 */
@Route(path = "/account/login", extras = RouteFlag.FLAG_LOGIN)
class LoginActivity : HiBaseActivity() {

    private val REQUEST_CODE_REGISTRATION = 1000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        HiStatusBar.setStatusBar(this, true, translucent = false)

        setContentView(R.layout.activity_login)

        action_back.setOnClickListener {
            onBackPressed()
        }

        action_login.setOnClickListener {
            goLogin()
        }

        action_register.setOnClickListener {
            goRegistration()
        }


    }

    /**
     * 通过阿里路由实现页面跳转
     */
    private fun goRegistration() {
        HiRoute.startActivity(
            this,
            destination = HiRoute.Destination.ACCOUNT_REGISTRATION,
            requestCode = REQUEST_CODE_REGISTRATION
        )
    }

    /**
     * 登录功能
     */
    private fun goLogin() {

        val name = input_item_username.getEditTextView().text
        val password = input_item_password.getEditTextView().text

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
            return
        }
        // 调用登录接口登录
        ApiFactory.create(AccountApi::class.java)
            .login(name.toString(), password.toString())
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(hiResponse: HiResponse<String>) {
                    if (hiResponse.code == HiResponse.SUCCESS) {
                        showToast(getString(R.string.login_success))
                        // 把登录结果保存，并且退出页面
                        val data = hiResponse.data
                        AccountManager.loginSuccess(data!!)

                        //SPUtil.putString("boarding-pass", hiResponse.data.toString())
                        setResult(Activity.RESULT_OK, Intent())
                        finish()
                    } else {
                        showToast(getString(R.string.login_failed) + hiResponse.msg)
                    }
                }

                override fun onFailed(throws: Throwable) {
                    showToast(getString(R.string.login_failed) + throws.message)
                }
            })
    }

    /**
     * 接收来自注册页面传递过来的userName
     * 回显到用户名输入框
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        //kotlin的and判断
        if ((resultCode == Activity.RESULT_OK) and (data != null) and (requestCode == REQUEST_CODE_REGISTRATION)) {
            val userName = data!!.getStringExtra("username")
            if (TextUtils.isEmpty(userName)) {
                input_item_username.getEditTextView().setText(userName)
            }
        }


    }


}